package com.telit.dispose.controller.resourceManagement;

import com.telit.common.entity.DataReturn;
import com.telit.dispose.entity.resourceManagement.Dict;
import com.telit.dispose.service.resourceManagement.DictService;
import com.telit.dispose.vo.resourceManagement.DictVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * Create by Chencf 2022/09/05
 */

@Api(tags = "数据字典")
@RestController
@RequestMapping("/dict")
public class DictController {

    @Autowired
    private DictService dictService;

    @ApiOperation("01--获取所有的顶层节点，不包括子节点")
    @GetMapping("/findDictByParentId")
    public DataReturn<List<Dict>> findDictByDictType(@RequestParam("parentId") Integer parentId) {
        List<Dict> dictList = dictService.findByParentId(parentId);
        return DataReturn.ok(dictList);
    }


    @ApiOperation("02--根据字典分类code，获取字典列表")
    @GetMapping("/findDict")
    public DataReturn<List<DictVO>> findDictByDictType(@RequestParam("code") String code) {
        List<DictVO> dictList = dictService.listByParentCode(code);
        return DataReturn.ok(dictList);
    }


    @ApiOperation("03--添加数据字典类型")
    @PostMapping("/findDictInsert")
    public DataReturn insert(@Validated @RequestBody Dict dict) {
        dictService.insert(dict);
        return DataReturn.ok();
    }


    @ApiOperation("04--根据字典父子节点code，获取字典列表")
    @GetMapping("/findDictInfoByCode")
    public DataReturn<DictVO> findDictInfoByCode(@RequestParam("parentCode") String parentCode,
                                                 @RequestParam("childCode") String childCode) {
        DictVO dictList = dictService.findDictInfoByCode(parentCode, childCode);
        return DataReturn.ok(dictList);
    }


    @ApiOperation("05--删除字典")
    @GetMapping("/deleteDictById")
    public DataReturn deleteDictById(@RequestParam("id") Long id) {
        dictService.deleteDictById(id);
        return DataReturn.ok();
    }

    //selectByDictId
    @ApiOperation("06--查询字典详情")
    @GetMapping("/selectByDictId")
    public DataReturn selectByDictId(@RequestParam("id") Long id) {
       Dict dict= dictService.selectByDictId(id);
        return DataReturn.ok(dict);
    }
}
